home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BSPLINE.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-01  |  10.4 KB  |  355 lines

  1. VERSION 4.00
  2. Begin VB.Form BsplineForm 
  3.    Caption         =   "B-spline"
  4.    ClientHeight    =   5430
  5.    ClientLeft      =   2175
  6.    ClientTop       =   930
  7.    ClientWidth     =   4830
  8.    Height          =   6120
  9.    Left            =   2115
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   362
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   300
  15.    Width           =   4950
  16.    Begin VB.CheckBox ShowTCheck 
  17.       Caption         =   "Show t Values"
  18.       Height          =   255
  19.       Left            =   1680
  20.       TabIndex        =   8
  21.       Top             =   300
  22.       Width           =   1755
  23.    End
  24.    Begin VB.TextBox KText 
  25.       Height          =   285
  26.       Left            =   1140
  27.       TabIndex        =   6
  28.       Text            =   "3"
  29.       Top             =   45
  30.       Width           =   375
  31.    End
  32.    Begin VB.CommandButton CmdNew 
  33.       Caption         =   "New"
  34.       Enabled         =   0   'False
  35.       Height          =   375
  36.       Left            =   4320
  37.       TabIndex        =   5
  38.       Top             =   0
  39.       Width           =   495
  40.    End
  41.    Begin VB.CommandButton CmdGo 
  42.       Caption         =   "Go"
  43.       Default         =   -1  'True
  44.       Enabled         =   0   'False
  45.       Height          =   375
  46.       Left            =   3600
  47.       TabIndex        =   4
  48.       Top             =   0
  49.       Width           =   495
  50.    End
  51.    Begin VB.CheckBox ControlCheck 
  52.       Caption         =   "Show Control Points"
  53.       Height          =   255
  54.       Left            =   1680
  55.       TabIndex        =   3
  56.       Top             =   0
  57.       Value           =   1  'Checked
  58.       Width           =   1755
  59.    End
  60.    Begin VB.TextBox DtText 
  61.       Height          =   285
  62.       Left            =   240
  63.       TabIndex        =   2
  64.       Text            =   "0.05"
  65.       Top             =   45
  66.       Width           =   615
  67.    End
  68.    Begin VB.PictureBox Canvas 
  69.       AutoRedraw      =   -1  'True
  70.       Height          =   4815
  71.       Left            =   0
  72.       ScaleHeight     =   317
  73.       ScaleMode       =   3  'Pixel
  74.       ScaleWidth      =   317
  75.       TabIndex        =   0
  76.       Top             =   600
  77.       Width           =   4815
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "K"
  81.       Height          =   255
  82.       Index           =   0
  83.       Left            =   960
  84.       TabIndex        =   7
  85.       Top             =   60
  86.       Width           =   255
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "dt"
  90.       Height          =   255
  91.       Index           =   1
  92.       Left            =   0
  93.       TabIndex        =   1
  94.       Top             =   60
  95.       Width           =   255
  96.    End
  97.    Begin VB.Menu mnuFile 
  98.       Caption         =   "&File"
  99.       Begin VB.Menu mnuFileExit 
  100.          Caption         =   "E&xit"
  101.       End
  102.    End
  103. Attribute VB_Name = "BsplineForm"
  104. Attribute VB_Creatable = False
  105. Attribute VB_Exposed = False
  106. Option Explicit
  107. Const PI = 3.14159
  108. Const GAP = 3
  109. ' The endpoints are points 1 and 4. The control
  110. ' points are points 2 and 3.
  111. Dim MaxPt As Integer
  112. Dim PtX() As Single
  113. Dim PtY() As Single
  114. Dim MakingNew As Boolean
  115. ' The index of the point being dragged.
  116. Dim Dragging As Integer
  117. Dim OldMode As Integer
  118. ' Kvalue determines the smoothness of the curve.
  119. Dim Kvalue As Integer
  120. ' t runs between 0 and MaxPt - Kvalue + 2.
  121. Dim MaxT As Single
  122. ' ************************************************
  123. ' Recursively compute the blending function.
  124. ' ************************************************
  125. Function Blend(i As Integer, k As Integer, t As Single) As Single
  126. Dim numer As Single
  127. Dim denom As Single
  128. Dim value1 As Single
  129. Dim value2 As Single
  130.     ' Base case for the recursion.
  131.     If k = 1 Then
  132.         If Knot(i) <= t And t < Knot(i + 1) Then
  133.             Blend = 1
  134.         ElseIf t = MaxT And Knot(i) <= t And t <= Knot(i + 1) Then
  135.             Blend = 1
  136.         Else
  137.             Blend = 0
  138.         End If
  139.         Exit Function
  140.     End If
  141.     denom = Knot(i + k - 1) - Knot(i)
  142.     If denom = 0 Then
  143.         value1 = 0
  144.     Else
  145.         numer = (t - Knot(i)) * Blend(i, k - 1, t)
  146.         value1 = numer / denom
  147.     End If
  148.     denom = Knot(i + k) - Knot(i + 1)
  149.     If denom = 0 Then
  150.         value2 = 0
  151.     Else
  152.         numer = (Knot(i + k) - t) * Blend(i + 1, k - 1, t)
  153.         value2 = numer / denom
  154.     End If
  155.     Blend = value1 + value2
  156. End Function
  157. ' ************************************************
  158. ' Draw the curve on the indicated picture box.
  159. ' ************************************************
  160. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, dt As Single)
  161. Dim x1 As Single
  162. Dim y1 As Single
  163. Dim t As Single
  164.     x1 = X(start_t)
  165.     y1 = Y(start_t)
  166.     pic.Cls
  167.     pic.CurrentX = x1
  168.     pic.CurrentY = y1
  169.     t = start_t + dt
  170.     Do While t < stop_t
  171.         x1 = X(t)
  172.         y1 = Y(t)
  173.         pic.Line -(x1, y1)
  174.         t = t + dt
  175.     Loop
  176.     x1 = X(stop_t)
  177.     y1 = Y(stop_t)
  178.     pic.Line -(x1, y1)
  179. End Sub
  180. ' ************************************************
  181. ' Return the ith knot value.
  182. ' ************************************************
  183. Function Knot(i As Integer) As Integer
  184.     If i < Kvalue Then
  185.         Knot = 0
  186.     ElseIf i <= MaxPt Then
  187.         Knot = i - Kvalue + 1
  188.     Else
  189.         Knot = MaxPt - Kvalue + 2
  190.     End If
  191. End Function
  192. ' ************************************************
  193. ' The parametric function Y(t).
  194. ' ************************************************
  195. Function Y(t As Single) As Single
  196. Dim i As Integer
  197. Dim value As Single
  198.     For i = 0 To MaxPt
  199.         value = value + PtY(i) * Blend(i, Kvalue, t)
  200.     Next i
  201.     Y = value
  202. End Function
  203. ' ************************************************
  204. ' The parametric function X(t).
  205. ' ************************************************
  206. Function X(t As Single) As Single
  207. Dim i As Integer
  208. Dim value As Single
  209.     For i = 0 To MaxPt
  210.         value = value + PtX(i) * Blend(i, Kvalue, t)
  211.     Next i
  212.     X = value
  213. End Function
  214. ' ************************************************
  215. ' Use DrawCurve to draw the Bezier curve.
  216. ' ************************************************
  217. Private Sub DrawBspline()
  218. Const DOTTED = 2
  219. Dim dt As Single
  220. Dim i As Integer
  221. Dim oldstyle As Integer
  222.     If MaxPt < 0 Then Exit Sub
  223.     MousePointer = vbHourglass
  224.     Kvalue = CInt(KText.Text)
  225.     dt = CSng(DtText.Text)
  226.     MaxT = MaxPt - Kvalue + 2
  227.     DrawCurve Canvas, 0, MaxT, dt
  228.     If ControlCheck.value = vbChecked Then
  229.         ' Draw the control points.
  230.         For i = 0 To MaxPt
  231.             Canvas.Line _
  232.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  233.                 Step(2 * GAP, 2 * GAP), , BF
  234.         Next i
  235.         
  236.         ' Connect the control points.
  237.         oldstyle = Canvas.DrawStyle
  238.         Canvas.DrawStyle = DOTTED
  239.         Canvas.CurrentX = PtX(0)
  240.         Canvas.CurrentY = PtY(0)
  241.         For i = 1 To MaxPt
  242.             Canvas.Line -(PtX(i), PtY(i))
  243.         Next i
  244.         Canvas.DrawStyle = oldstyle
  245.     End If
  246.     ' Mark the t values if desired.
  247.     If ShowTCheck.value = vbChecked Then
  248.         For dt = 0 To MaxT Step 1#
  249.             Canvas.Line (X(dt), Y(dt) - 5)-Step(0, 10)
  250.             Canvas.Line (X(dt) - 5, Y(dt))-Step(10, 0)
  251.         Next dt
  252.     End If
  253.     MousePointer = vbDefault
  254. End Sub
  255. ' ************************************************
  256. ' Either collect a new point or select a point and
  257. ' start dragging it.
  258. ' ************************************************
  259. Private Sub Canvas_MouseDown(button As Integer, Shift As Integer, X As Single, Y As Single)
  260. Dim i As Integer
  261.     ' If we are selecting points, do so now.
  262.     If MakingNew Then
  263.         MaxPt = MaxPt + 1
  264.         ReDim Preserve PtX(0 To MaxPt)
  265.         ReDim Preserve PtY(0 To MaxPt)
  266.         PtX(MaxPt) = X
  267.         PtY(MaxPt) = Y
  268.         Canvas.Line _
  269.             (X - GAP, Y - GAP)- _
  270.             Step(2 * GAP, 2 * GAP), , BF
  271.         
  272.         If MaxPt >= 3 Then CmdGo.Enabled = True
  273.         
  274.         Exit Sub
  275.     End If
  276.     ' Otherwise start dragging a point.
  277.     ' Find a close point.
  278.     For i = 0 To MaxPt
  279.         If Abs(PtX(i) - X) <= GAP And _
  280.            Abs(PtY(i) - Y) <= GAP Then Exit For
  281.     Next i
  282.     If i > MaxPt Then Exit Sub
  283.     Dragging = i
  284.     OldMode = Canvas.DrawMode
  285.     Canvas.DrawMode = vbInvert
  286.     PtX(Dragging) = X
  287.     PtY(Dragging) = Y
  288.     Canvas.Line _
  289.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  290.         Step(2 * GAP, 2 * GAP), , BF
  291. End Sub
  292. ' ************************************************
  293. ' Continue dragging a point.
  294. ' ************************************************
  295. Private Sub Canvas_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
  296.     If Dragging < 0 Then Exit Sub
  297.     Canvas.Line _
  298.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  299.         Step(2 * GAP, 2 * GAP), , BF
  300.     PtX(Dragging) = X
  301.     PtY(Dragging) = Y
  302.     Canvas.Line _
  303.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  304.         Step(2 * GAP, 2 * GAP), , BF
  305. End Sub
  306. ' ************************************************
  307. ' Finish the drag and redraw the curve.
  308. ' ************************************************
  309. Private Sub Canvas_MouseUp(button As Integer, Shift As Integer, X As Single, Y As Single)
  310.     If Dragging < 0 Then Exit Sub
  311.     Canvas.DrawMode = OldMode
  312.     PtX(Dragging) = X
  313.     PtY(Dragging) = Y
  314.     Dragging = -1
  315.     DrawBspline
  316. End Sub
  317. Private Sub CmdGo_Click()
  318.     MakingNew = False
  319.     CmdNew.Enabled = True
  320.     DrawBspline
  321. End Sub
  322. ' ************************************************
  323. ' Prepare to get new points.
  324. ' ************************************************
  325. Private Sub CmdNew_Click()
  326.     MaxPt = -1
  327.     CmdGo.Enabled = False
  328.     CmdNew.Enabled = False
  329.     MakingNew = True
  330.     Canvas.Cls
  331. End Sub
  332. Private Sub ControlCheck_Click()
  333.     DrawBspline
  334. End Sub
  335. Private Sub Form_Load()
  336.     MakingNew = True
  337.     MaxPt = -1
  338.     Dragging = -1
  339. End Sub
  340. ' ************************************************
  341. ' Make the canvas as big as possible.
  342. ' ************************************************
  343. Private Sub Form_Resize()
  344.     Canvas.Move 0, Canvas.Top, _
  345.         ScaleWidth, ScaleHeight - Canvas.Top
  346.         
  347.     DrawBspline
  348. End Sub
  349. Private Sub mnuFileExit_Click()
  350.     Unload Me
  351. End Sub
  352. Private Sub ShowTCheck_Click()
  353.     DrawBspline
  354. End Sub
  355.